<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-03-10 20:47:59
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_categorys extends discuz_table
{
    public function __construct()
    {
        $this->_table         = 'jamesonread_categorys';
        $this->_pk            = 'category_id';
        $this->_pre_cache_key = 'jamesonreadcategorys_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
/*取得子分类*/
    public function fetch_all_parent($parentid=0)
    {
        $cacheid = "allparent-".$parentid;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = DB::fetch_all("SELECT * FROM %t WHERE parent_id=%d ORDER BY ordernum ", array($this->_table, $parentid),'category_id');
        if ($data) {
            $this->store_cache($cacheid, $data);
        }
        return $data;
    }
/*取得所有分类*/
    public function fetch_all_cate()
    {
        $cacheid = "fetch_all_cate";
        if($cache = $this->fetch_cache($cacheid)){
            return $cache;
        }
        $data = DB::fetch_all("SELECT * FROM %t ORDER BY parent_id,ordernum", array($this->_table), 'category_id');
        $tmp  = $keytmp  = $sortkey  = $subsortkey  = array();
        foreach ($data as $key => $value) {
            if ($value['parent_id'] < 1) {
                $tmp[$key] = $value;
                $sortkey[] = $value['ordernum'];
                unset($data[$value['category_id']]);
            } else {
                $subsortkey[] = $value['ordernum'];
            }
        }
        foreach ($tmp as $key => $value) {
            $keytmp[$value['category_id']] = $value;
        }
        array_multisort($subsortkey, SORT_ASC, $data, SORT_ASC);
        foreach ($data as $key => $value) {
            if ($keytmp[$value['parent_id']]) {
                $keytmp[$value['parent_id']]['sub'][] = $value;
            }
        }
        array_multisort($sortkey, SORT_ASC, $keytmp, SORT_ASC);
        $this->store_cache($cacheid,$keytmp);
        return $keytmp;
    }
    public function fetch_categoryid()
    {
        if ($cache = $this->fetch_cache('allcategory_id')) {
            return $cache;
        }
        $data = DB::fetch_all('SELECT category_id FROM %t WHERE parent_id>%d',array($this->_table,0),'category_id');
        $data = array_keys($data);
        $this->store_cache('allcategory_id',$data);
        return $data;
    }
/*取得父级分类id*/
    public function fetch_pid_byid($category_id)
    {
        $cacheid = 'fetch_pid_byid-'.$category_id;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = DB::result_first('SELECT parent_id FROM %t WHERE category_id=%d', array($this->_table, $category_id));
        if ($data) {
            $this->store_cache($cacheid, $data);
        }
        return $data;
    }
    public function fetch_name($category_id)
    {
        $cacheid = 'fetch_name-'.$category_id;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = DB::result_first("SELECT category_name FROM %t WHERE category_id=%d", array($this->_table, $category_id));
        if ($data) {
            $this->store_cache($cacheid, $data);
        }
        return $data;
    }
    // by cid
    public function fetch_row_bycid($category_id)
    {
        $cacheid = 'fetch_row_bycid-'.$category_id;
        if ($cache = $this->fetch_cache($cacheid)) {
            return $cache;
        }
        $data = DB::fetch_first("SELECT * FROM %t WHERE category_id=%d", array($this->_table, $category_id));
        if ($data) {
            $this->store_cache($cacheid, $data);
        }
        return $data;
    }
}